package com.hsgene.product.controller.applets;

import com.hsgene.common.util.qiniu.QiniuCertificateUtil;
import com.hsgene.common.util.resolver.IgnoreUserToken;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.product.domain.GeneticBanners;
import com.hsgene.product.exception.ProductErrorCode;
import com.hsgene.product.service.GeneticBannersService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @projectName: hdas-geneshop-server
 * @package: com.hsgene.product.controller
 * @author: maodi
 * @createDate: 2018/10/11 12:00
 * @version: 1.0
 * Copyright: Copyright (c) 2018
 */
@RestController
@RequestMapping("/v1.0/genetic/testing")
public class GeneticBannersController {

    private final static Logger LOGGER = LogManager.getLogger(GeneticBannersController.class);

    @Autowired
    private GeneticBannersService geneticBannersService;

    /**
     * @param
     * @return java.util.List<com.hsgene.basic.domain.GeneticBanners>
     * @description 获取首页下面的9张图片
     * @author maodi
     * @createDate 2018/10/11 13:43
     */
    @IgnoreUserToken(platform = 4)
    @GetMapping("home/images")
    public ApiResult<List<GeneticBanners>> getImages() {
        try {
            List<GeneticBanners> list = geneticBannersService.getImages();
            if (list == null || list.size() < 0) {
                return ApiResult.succ(new ArrayList<>());
            }
            list.sort((GeneticBanners o1, GeneticBanners o2) -> o2.getSort() - o1.getSort());
            for (GeneticBanners banner : list) {
                String oldUrl = banner.getImageUrl();
                String newUrl = QiniuCertificateUtil.getDownloadCertificateStr(oldUrl);
                banner.setImageUrl(newUrl);
            }
            return ApiResult.succ(list);
        } catch (Exception e) {
            LOGGER.error(ProductErrorCode.GET_IMAGES_IS_ERROR.getMessage(), e);
            return ApiResult.fail(ProductErrorCode.GET_IMAGES_IS_ERROR);
        }
    }

}
